package com.example.myrecyclerview.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myrecyclerview.MainActivity;
import com.example.myrecyclerview.R;
import com.example.myrecyclerview.RecyclerViewAdapter;
import com.example.myrecyclerview.bean.Book;

import java.util.ArrayList;

public class BookFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_book, container, false);
        mLayoutManager = new LinearLayoutManager(MainActivity.getContext(), LinearLayoutManager.VERTICAL, false);
        mAdapter = new RecyclerViewAdapter(getListBooks());
        mRecyclerView = (RecyclerView) root.findViewById(R.id.recycle_view_books);
        // 设置布局管理器
        mRecyclerView.setLayoutManager(mLayoutManager);
        // 设置adapter
        mRecyclerView.setAdapter(mAdapter);
//        // 设置Item添加和移除的动画
//        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//        // 设置Item之间间隔样式
//        mRecyclerView.addItemDecoration(mDividerItemDecoration);
//        DataManager dataManager = new DataManager();
//        dataManager.writeBookByStream(getListBooks());
//        ArrayList<Book> newBooks = null;
//        try {
//            newBooks = dataManager.readBookByStream();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        for(Book book:newBooks){
//            Log.d("<Test>","读取到数据："+book.getName());
//        }
        return root;
    }


    private ArrayList<Book> getListBooks() {

        ArrayList<Book> mListBooks = new ArrayList<Book>();

        mListBooks.add(new Book("软件项目管理案例教程（第4版）", R.drawable.book_2));
        mListBooks.add(new Book("创新工程实践", R.drawable.book_no_name));
        mListBooks.add(new Book("信息安全数学基础（第2版）", R.drawable.book_1));

        return mListBooks;
    }
}
